using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wicresoft.SMS.Send
{
    public partial class Send : Form
    {
        public Send()
        {
            InitializeComponent();
        }


        private void Send_Load(object sender, EventArgs e)
        {
            this.txtUrl.Text = ConfigManage.ProviderUrl;
            CheckInput();
        }

        private void btnTo_Click(object sender, EventArgs e)
        {
            try
            {
                AddressBook frmab = new AddressBook();
                frmab.TO = this.txtTos.Text;
                frmab.ShowDialog();
                if (frmab.IsOK)
                    this.txtTos.Text = frmab.TO;
            }
            catch (Exception ex)
            {
                Logger.Exception("Send.btnTo_Click ", ex);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string result = SendSMS.Send(this.txtTos.Text, this.txtContent.Text);
                if (result != "Unable to connect to the remote server")
                    if (CheckResult(result))
                    {
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Send Messages Failed", "Failed");
                    }
                else
                    MessageBox.Show("Unable to connect to the remote server", "Failed");
                    
            }
            catch (Exception ex)
            {
                Logger.Exception("Send.btnSend_Click ", ex);
            }
        }

        private void CheckInput()
        {
            if (this.txtContent.Text.Trim() == "" || this.txtTos.Text.Trim() == "")
            {
                this.btnSend.Enabled = false;
            }
            else
                this.btnSend.Enabled = true;
        }

        private void txtTos_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private void txtContent_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private bool CheckResult(string result)
        {
            if (result.Contains("code=\"ok\""))
                return true;
            else
                return false;
        }
    }
}